﻿#include "CBeltWorkBase.h"
#include <QEventLoop>
#include <QTimer>
#include <QTime>
#include "CBeltSqlOper.h"
CBeltWorkBase::CBeltWorkBase(CBeltDataBase *pBeltData)
{
	m_pBeltData = pBeltData;
	m_pThread = new QThread();
	this->moveToThread(m_pThread);
	connect(m_pThread, SIGNAL(started()), this, SLOT(slot_threadWork()));
}


CBeltWorkBase::~CBeltWorkBase()
{
	threadStop();
	if (m_pThread != nullptr)
	{
		delete m_pThread;
		m_pThread = nullptr;
	}
}

void CBeltWorkBase::threadStart()
{
	m_pThread->start();
}

void CBeltWorkBase::threadStop()
{
	m_bhread = false;
	m_pThread->quit();
	if (!m_pThread->wait(5000))
	{
		m_pThread->terminate();
	}
}

void CBeltWorkBase::beltTearExec()
{
	/**
	* @ Author: Gaojia
	* @ Description: 判断皮带撕裂是否需要检测
	*/
	if (!beltIsCheck())
	{
		return;
	}
	/**
	* @ Author: Gaojia
	* @ Description: 获取检测数据
	*/
	selectBeltData(m_pBeltData);
}

bool CBeltWorkBase::beltIsCheck()
{
	bool bResult = false;
	//如果手动停止检测  返回不检测
	if (!m_pBeltData->m_bBeltWork)
		return false;
	//获取是否到雨刮动作时间 判断雨刮器是否工作
	QTime currentTime = QTime::currentTime();
	int nMimute = (currentTime.hour() * 60 + currentTime.minute()) % 90;
	if (nMimute < 3 || nMimute > 87)
	{
		m_pBeltData->m_bWipersWork = true;
		bResult = false;
	}
	else
	{
		m_pBeltData->m_bWipersWork = false;
		bResult = true;
	}
	return bResult;
}

void CBeltWorkBase::slot_threadWork()
{
	m_bhread = true;
	while (m_bhread)
	{
		if (m_pBeltData->m_mapInputPoint.size() > 0)
		{
			m_pBeltData->m_mapInputPoint["0"]->setValue(1);
			m_pBeltData->m_mapInputPoint["1"]->setValue(1);
		}	
		beltTearExec();
		QEventLoop loop;
		QTimer::singleShot(2000, &loop, SLOT(quit()));
		loop.exec();
	}
}
